import axios from 'axios'
import router from '../routes.js'
import env from './env';
import qs from "qs";

// 创建axios 实例
const service = axios.create({
    timeout: 50000,
    loading: true,
	baseURL: env.baseURL
})
 
//请求拦截器
service.interceptors.request.use(
    config => {
        if (config.method === "get") {
            config.data = {
                unused: 0
            }; // 这个是关键点，加入这行就可以解决get请求添加不上Content-Type
        }
		
        config.headers["Content-type"] = "application/json;charset=UTF-8";
		const cType = config.headers["Content-Type"]
		if (cType === "application/x-www-form-urlencoded") {
			config.data = qs.stringify(config.data)
		}
		if (localStorage.getItem('token')) {
			config.headers.token = localStorage.getItem('token');
		}
        return config;
    },
    error => {
        Promise.reject(error);
    }
);
 
//响应拦截器
service.interceptors.response.use(response => {
    return response;
})
 
export default service